package com.yj_app.qualitytest.base.mvp;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.CallSuper;

import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.yj_app.qualitytest.info.up.MsgBean;
import com.yj_app.qualitytest.util.GsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;

public abstract class BasePresenterImpl<V extends BaseView,M extends BaseModel> implements BasePresenter{
    protected V mView;
    protected M mModel;


    protected abstract M createModel();

    @Override
    public void attachView(BaseView view) {
        this.mView= (V) view;
        if (mModel == null) {
            mModel = createModel();
        }
    }

    @CallSuper
    public void detachView() {
        mModel = null;
        mView = null;
    }



    public M getModel() {
        return mModel;
    }


    public String getMsgBean(String res) {

        if (TextUtils.isEmpty(res)) {

            return res;
        }
        if (res.indexOf("<return>") > 0)
            res = res.substring(res.indexOf("<return>") + 8, res.indexOf("</return>"));
        else
            res = res.substring(res.indexOf("ns1:return") + 11, res.indexOf("</ns1:return>"));

        return res;

    }


    public String data2json(String method, String gsid) {

        MsgBean msgBean = new MsgBean();

        msgBean.setMethod(Integer.valueOf(method));
        msgBean.setPk_org(gsid);
        return getSoapstr(GsonUtil.toJson(msgBean));

    }
    public String msg2json(MsgBean msgBean) {

        return getSoapstr(GsonUtil.toJson(msgBean));

    }

    static String getSoapstr(String jsonstr) {

        return "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:iqu=\"http://ws.jshmy.itf.nc/IQuality\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <iqu:executeBill>\n" +
                "         <!--Optional:-->\n" +
                "         <iqu:string>" + cnToUnicode(jsonstr) + "</iqu:string>\n" +
                "      </iqu:executeBill>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";
    }

    private static String cnToUnicode(String cn) {
        char[] chars = cn.toCharArray();
        String returnStr = "";
        for (int i = 0; i < chars.length; i++) {
            returnStr += "\\u" + Integer.toString(chars[i], 16);
        }
        return returnStr;
    }
}
